package com.zhangwei.day43bluetooth;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.zhangwei.day43bluetooth.adapter.DeviceAdapter;

import java.util.ArrayList;

/**
 * 4.0之前的蓝牙
 */
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener, View.OnClickListener {
    BluetoothAdapter bluetoothAdapter;//代表本地的蓝牙设备
    Button startBtn;
    Button disBtn;
    Button scanDev;
    ListView listView;
    private DeviceAdapter adapter;
    private DevReceiver devReceiver = new DevReceiver();
    private ArrayList<BluetoothDevice> devices = new ArrayList<>();

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startBtn = (Button) findViewById(R.id.start_btn);
        disBtn = (Button) findViewById(R.id.dis_btn);
        scanDev = (Button) findViewById(R.id.scan_dev);
        listView = (ListView) findViewById(R.id.listView);
        listView.setOnItemClickListener(this);
        listView.setAdapter(adapter = new DeviceAdapter(this, devices));
        startBtn.setOnClickListener(this);
        disBtn.setOnClickListener(this);
        scanDev.setOnClickListener(this);
        init();
    }

    private void init() {
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    }

    public void open() {
        if (bluetoothAdapter != null && !bluetoothAdapter.isEnabled()) {
            //打开蓝牙有两种方法
            bluetoothAdapter.enable();
//            Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
//            startActivity(intent);
        }
    }


    /**
     * 设置可检测性时间
     */
    private void setdiscoverTime(int time) {
        Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, time);
        startActivity(intent);
    }

    /**
     * 扫描设备
     */
    private void scanDev() {
        //动态注册
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
        registerReceiver(devReceiver, intentFilter);
        bluetoothAdapter.startDiscovery();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (devReceiver != null) {
            unregisterReceiver(devReceiver);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start_btn:
                open();
                break;
            case R.id.dis_btn:
                setdiscoverTime(250);
                break;
            case R.id.scan_dev:
                scanDev();
                break;

        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        bluetoothAdapter.cancelDiscovery();//停止扫描设备
        BluetoothDevice device = devices.get(position);
    }


    public class DevReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null && intent.getAction().equals(BluetoothDevice.ACTION_FOUND)) {
                BluetoothDevice device = intent.getExtras().getParcelable(BluetoothDevice.EXTRA_DEVICE);
//                String name = device.getName();
//                String addr = device.getAddress();
                devices.add(device);
                adapter.notifyDataSetChanged();
            }
        }
    }

}
